package com.iafme.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.EnterLevelReqDTO;
import com.iafme.dto.res.EnterLevelResDTO;
import com.iafme.model.Level;
import com.iafme.service.ILevelService;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @version 1.0
 * @date 2025/1/7 18:00
 */
@RestController
@RequestMapping(value = "/level")
@AllArgsConstructor
public class EnterLevelController implements BaseCommonInterface<EnterLevelReqDTO,EnterLevelResDTO> {

    private final ILevelService levelService;

    @OperateAnnotation(businessType = BusinessType.INSERT,name = "报考等级")
    @SaCheckPermission("enter:level:add")
    @Override
    public Result<String> save(@Valid EnterLevelReqDTO enterLevelReqDTO) {
        Level convert = Convert.convert(Level.class, enterLevelReqDTO);
        levelService.save(convert);
        return Result.successResult();
    }
    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "报考等级")
    @SaCheckPermission("enter:level:edit")
    @Override
    public Result<String> update(@Valid EnterLevelReqDTO enterLevelReqDTO) {
        Level convert = Convert.convert(Level.class, enterLevelReqDTO);
        levelService.updateById(convert);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.DELETE,name = "报考等级")
    @SaCheckPermission("enter:level:delete")
    @Override
    public Result<String> del(Integer id) {
        levelService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<EnterLevelResDTO>> lists(EnterLevelReqDTO enterLevelReqDTO) {

        return Result.successResult(levelService.lists(enterLevelReqDTO));
    }

    @Override
    public Result<EnterLevelResDTO> findById(Integer id) {
        Level level = levelService.getById(id);
        EnterLevelResDTO enterLevelResDTO = Convert.convert(EnterLevelResDTO.class, level);
        return Result.successResult(enterLevelResDTO);
    }

    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "报考等级")
    @SaCheckPermission("enter:level:delete")
    @Override
    public Result<String> batchDel(List<EnterLevelResDTO> list) {
        List<Integer> ids = list.stream().map(EnterLevelResDTO::getId).collect(Collectors.toList());
        levelService.removeByIds(ids);
        return Result.successResult();
    }

    @GetMapping(value = "/findAll/{projectId}")
    public List<EnterLevelResDTO> findAll(@PathVariable Integer projectId){
        LambdaQueryWrapper<Level> eq = Wrappers.lambdaQuery(Level.class).eq(Level::getProjectId, projectId);
        List<Level> list = levelService.list(eq);
        return Convert.toList(EnterLevelResDTO.class, list);
    }
}
